#include "cadrechecktab.h"
#include "ui_cadrechecktab.h"
#include <QDialog>
#include <QGridLayout>
#include <QLineEdit>
#include <QSqlQuery>
#include <QPushButton>
#include <QSqlTableModel>
#include <QDebug>
#include <QLabel>
#include <QDialogButtonBox>
#include <QMessageBox>
#include <QSqlError>
#include "cadreutil.h"

CadreCheckTab::CadreCheckTab(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CadreCheckTab)
{
    ui->setupUi(this);
}

CadreCheckTab::~CadreCheckTab()
{
    delete ui;
}
void CadreCheckTab::initCheckTableView(QString curPerson)
{
    checkModel = new QSqlTableModel(this);
    checkModel->setTable("`Check`"); //check 数据库的特殊字符，得加``
    checkModel->setEditStrategy(QSqlTableModel::OnFieldChange);// OnRowChange OnFieldChange OnManualSubmit
    QString filter = QString::fromUtf8("IDNum='%1'").arg(curPerson);
    currentPerson=curPerson;
    checkModel->setFilter(filter);
    if (checkModel->select())
    {
        checkModel->setHeaderData(1, Qt::Horizontal, QString::fromUtf8("年度"));
        checkModel->setHeaderData(2, Qt::Horizontal, QString::fromUtf8("审核结果"));
        checkModel->setHeaderData(4, Qt::Horizontal, QString::fromUtf8("考核项目"));
        checkModel->setHeaderData(5, Qt::Horizontal, QString::fromUtf8("考核内容"));
        ui->checkTableView->horizontalHeader()->setVisible(true); //UI界面设置，总是恢复为false
        ui->checkTableView->setModel(checkModel);
        ui->checkTableView->setColumnHidden(0,true);
        ui->checkTableView->setColumnHidden(3,true);
        ui->checkTableView->setColumnWidth(1,130);
        ui->checkTableView->setColumnWidth(2,150);
        ui->checkTableView->setColumnWidth(4,150);
        ui->checkTableView->setColumnWidth(5,200);
        ui->checkTableView->selectRow(checkModel->rowCount()-1);
    }else{
        qDebug()<< checkModel->lastError();
    }


}
void CadreCheckTab::setButtonEnable(bool i)
{
    ui->checkAddButton->setEnabled(i);
    ui->checkDeleteButton->setEnabled(i);
    ui->checkDownButton->setEnabled(i);
    ui->checkUpButton->setEnabled(i);
    if(i){
        ui->checkTableView->setEditTriggers(QAbstractItemView::DoubleClicked);
    }else{
        ui->checkTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}

void CadreCheckTab::on_checkAddButton_clicked()
{
    QDialog *addDialog = new QDialog(this);
    addDialog->setWindowTitle(trUtf8("添加年度考核"));
    addDialog->setGeometry(700,300,400,380);
    addDialog->setContentsMargins(10,0,10,0);
    QGridLayout *gridLayout = new QGridLayout(addDialog);
    gridLayout->setMargin(10);

    QLabel *yearLabel =new QLabel(trUtf8("年度"),addDialog);
    QLabel *resultLabel=new QLabel(trUtf8("审核结果"),addDialog);
    QLabel *itemLabel=new QLabel(trUtf8("考核项目"),addDialog);
    QLabel *contentLabel=new QLabel(trUtf8("考核内容"),addDialog);

    QLineEdit *year = new QLineEdit(addDialog);
    QLineEdit *result = new QLineEdit(addDialog);
    QLineEdit *item = new QLineEdit(addDialog);
    QLineEdit *content = new QLineEdit(addDialog);

    gridLayout->addWidget(yearLabel,0,0);
    gridLayout->addWidget(year,0,1);
    gridLayout->addWidget(resultLabel,1,0);
    gridLayout->addWidget(result,1,1);
    gridLayout->addWidget(itemLabel,2,0);
    gridLayout->addWidget(item,2,1);
    gridLayout->addWidget(contentLabel,3,0);
    gridLayout->addWidget(content,3,1);

    QPushButton *yesButton = new QPushButton(trUtf8("确定"));
    QPushButton *cancelButton = new QPushButton(trUtf8("取消"));

    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal);
    buttonBox->addButton(yesButton, QDialogButtonBox::AcceptRole);
    buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);

    gridLayout->addWidget(buttonBox,4,0,1,2,Qt::AlignHCenter);

    connect(buttonBox, SIGNAL(accepted()), addDialog, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), addDialog, SLOT(reject()));

    if(addDialog->exec()==QDialog::Accepted){
        QSqlQuery query;
        query.prepare("insert into `Check` (`CheckYear`, `CheckResult`,"
                      " `IDNum`, `CheckItem`, `CheckContent`)"
                      " values(:year, :result, :idnum, :item, :content)");
        query.bindValue(":year",year->text());
        query.bindValue(":result",result->text());
        query.bindValue(":idnum",currentPerson);
        query.bindValue(":item",item->text());
        query.bindValue(":content",content->text());

        if(query.exec()){
            QMessageBox::information(this,trUtf8("提示"),
               trUtf8("插入数据成功"),QMessageBox::Yes);
            initCheckTableView(currentPerson);
        }else{
            QString info = query.lastError().databaseText();
            QMessageBox::information(this,trUtf8("插入数据库错误"),
               info,QMessageBox::Yes);
        }
    }
}

void CadreCheckTab::on_checkDeleteButton_clicked()
{
    CadreUtil::deleteRow(checkModel, ui->checkTableView);
}

void CadreCheckTab::on_checkUpButton_clicked()
{
    int cur = CadreUtil::moveUpOne(checkModel,0,ui->checkTableView);
    initCheckTableView(currentPerson);
    ui->checkTableView->selectRow(cur);
}

void CadreCheckTab::on_checkDownButton_clicked()
{
    int cur = CadreUtil::moveDownOne(checkModel,0,ui->checkTableView);
    initCheckTableView(currentPerson);
    ui->checkTableView->selectRow(cur);
}

